package com.molyfun.parents.activity.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.coursecenter.model.SubjectListResponse;
import com.molyfun.parents.activity.store.adapter.ProductAdapter;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import java.lang.reflect.Field;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 */
public class SearchResultActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{


    public static final String FLAG_SEARCH_STR = "search_str";


    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.record_list)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;

    private ProductAdapter mProductAdapter;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    private String mSearchStr;

    public static final void startActivity(Context context, String searchStr) {
        Intent intent = new Intent();
        intent.setClass(context, SearchResultActivity.class);
        intent.putExtra(FLAG_SEARCH_STR, searchStr);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        getSupportActionBar().setTitle("");
        ButterKnife.inject(this);

        mBack.setOnClickListener(this);
        mTitle.setText("搜索结果");

        mSearchStr = getIntent().getStringExtra(FLAG_SEARCH_STR);

        mProductAdapter = new ProductAdapter(SearchResultActivity.this);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);
        mRecyclerView.setAdapter(mProductAdapter);

        try {
            Field f = null;
            f = SwipeRefreshLayout.class.getDeclaredField("mTouchSlop");
            f.setAccessible(true);
            f.set(mSwipeRefreshLayout, 150);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        searchTopic(true);

    }

    @Override
    public void onRefresh() {
        searchTopic(true);
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
    }

    @Override
    public void onLoad() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        if (mHasMoreData) {
            mCurrentPage++;
            searchTopic(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void searchTopic(final boolean isRefresh) {
        showProgressDialog();
        Call<SubjectListResponse> call = MFPApplication.getInstance().getRestClient().getSubjectList(mCurrentPage, RestClient.DEFAULT_PAGE_SIZE, "", "" , "", "", "", "", mSearchStr);
        call.enqueue(new Callback<SubjectListResponse>() {
            @Override
            public void onResponse(Call<SubjectListResponse> call, Response<SubjectListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {

                    if(isRefresh){
                        if(response.body().getData().size() > 0){
                            mProductAdapter.refreshData(response.body().getData());
                            mHasMoreData = true;
                            mEmptyLayout.setVisibility(View.GONE);
                        }else{
                            mHasMoreData = false;
                            mProductAdapter.clearAll();
                            mEmptyLayout.setVisibility(View.VISIBLE);
                        }
                    }else{
                        if(response.body().getData().size() > 0){
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                        }
                        mProductAdapter.addAll(response.body().getData());
                    }

                }
            }

            @Override
            public void onFailure(Call<SubjectListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
